package lx.prac.kafka.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

public class ProducerOne {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        KafkaProducer<String,String> producer = getStringProducer();

        String batchId = UUID.randomUUID().toString();
        ProducerRecord<String, String> record = new ProducerRecord<>("my-repli","my-repli-key",batchId + "my-repli-content111");
        final RecordMetadata recordMetadata = producer.send(record).get();
        ProducerRecord<String, String> record2 = new ProducerRecord<>("my-repli",batchId + "my-repli-content222");
        producer.send(record2).get();
        ProducerRecord<String, String> record3 = new ProducerRecord<>("my-repli","my-repli-key",batchId + "my-repli-content333");
        producer.send(record3);
        System.out.println("******************begin*************");
//        System.out.println(recordMetadata.toString());
        System.out.println("******************end*************");

    }

    private static <T,V>  KafkaProducer getStringProducer() {
        Properties kafkaProps = new Properties();
        kafkaProps.put("bootstrap.servers","10.211.55.22:9091,10.211.55.22:9093");
        kafkaProps.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        kafkaProps.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

        return new KafkaProducer<T,V>(kafkaProps);
    }
}
